<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-card :style="{'min-height': containerHeight + 'px'}" class="box-card">
            <div slot="header" class="clearfix">
                <!-- 搜索表单 -->
                <el-form :model="query" ref="searchRuleForm" label-width="100px">
                    <el-row style="margin-top:20px">
                        <el-col :sm="20" :md="5">
                            <el-form-item label="分类类别:" prop="departNo">
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="query.record"
                                    size="small"
                                    class="handle-input mr10"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5" style="margin-left:10px">
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                @click="submitForm('searchRuleForm')"
                            >搜 索</el-button>
                            <el-button type="info" @click="resetForm('searchRuleForm')">重 置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
             <!-- 表格 -->
            <table-List
                :tableData="tableData"
                :tableHeader="tableHeader"
                :loading="loading"
                :pagination="pagination"
                @pageChange="pageChange"
                @selectFun="selectFun"
                @sortChange="sortChange"
            ></table-List>

        </el-card>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                record: '', //  部门名称
            },
            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 60
            },
            tableHeader: [
                // 表头数据
                // { prop: 'id', label: '序号', minWidth: '100px' },
                { prop: 'typeName', label: '分类名称', minWidth: '100px', sortable: 'custom' },
                { prop: 'description', label: '描述', minWidth: '100px', sortable: 'custom' },
            ],
            tableData: [],
            editData: {}, // 要修改的数据
            isEdit: false,
            multipleSelection: [],
            showEditDialog: false,
            departList: [] //部门下拉框数据
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        // 选择姓名
        showTreeDialog(tit) {
            console.log('showTreeDialog');
            this.searchDialogTit = tit;
            this.showSearch = true;
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                    this.findAllPage();
                })
                .catch(() => {});
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.findAllPage();
        },
        deleteById(id) {
            appMain
                .resignationDelById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //查询
        findAllPage() {
            this.loading = true;
            let params = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query.record
            };         
            appMain
                .dictTypeFindAllPageByTypeName(params)
                .then(res => {
                    this.timer = setTimeout(() => {
                        //设置延迟执行
                        this.loading = false;
                    }, 1000);
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        
                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.$message.error(res.data.message);
                        this.loading = false;
                    }
                })
                .catch(err => {
                    console.log(err);
                    this.timer = setTimeout(() => {
                        //设置延迟执行
                        this.loading = false;
                    }, 1000);
                });
        },
        //分页导航
        pageChange(data) {
            this.pagination = data.backData;
            console.log('pageChange:', this.pagination);
            //分页变化--请求接口
            this.findAllPage();
        },
        //树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.query.resignationUserName = data.backData.name;
            this.query.resignationUserId = data.backData.id;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.findAllPage();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created() {
        this.findAllPage();
    }
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}
</style>